# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

package require tcltest 2
namespace import tcltest::*

source ../macports_test_autoconf.tcl
package require macports 1.0

# Debug options
array set ui_options {}
#set ui_options(ports_debug)   yes
#set ui_options(ports_verbose) yes
mportinit ui_options

source ../macports_dlist.tcl


test dlist_match_multi {
    Dlist match multi unit test.
} -setup {
    set mport [mportopen file://[pwd]]
    set crit [list \
        provides gcc_select \
        porturl file://[pwd] \
    ]
    set crit2 { provides fondu2 }

} -body {
    if {[dlist_match_multi $mport $crit] ne $mport} {
        return "FAIL: correct dlist not matched"
    }
    if {[dlist_match_multi $mport $crit2] ne ""} {
        return "FAIL: incorrect dlist matched"
    }
    return "dlist match successful."
} -cleanup {
    mportclose $mport
} -result "dlist match successful."


test dlist_search {
    Dlist search unit test.
} -setup {
    set mport [mportopen file://.]
} -body {
    if {[dlist_search $mport provides gcc_select] ne $mport} {
        return "FAIL: matching item not found"
    }
    if {[dlist_search $mport provides fondu2] ne ""} {
        return "FAIL: wrong item detected"
    }
    return "dlist successful."
} -cleanup {
    mportclose $mport
} -result "dlist successful."


test dlist_delete {
    Dlist delete unit test.
} -setup {
    set mport [mportopen file://.]
    set dlist [list]
    lappend dlist $mport
} -body {
    if {[dlist_delete dlist $mport] ne ""} {
        return "FAIL: cannot run dlist_delete"
    }
    if {$dlist ne ""} {
        return "FAIL: port not removed from list"
    }
    return "Dlist delete successful."
} -cleanup {
    mportclose $mport
} -result "Dlist delete successful."


test dlist_has_pending {
    Dlist has pending unit test.
} -setup {
    set mport [mportopen file://.]
} -body {
    if {[dlist_has_pending $mport gcc_select] != 1} {
        return "FAIL: not detected"
    }
    if {[dlist_has_pending $mport provides] != 0} {
        return "FAIL: incorrect detected"
    }
    return "dlist pending successful."
} -cleanup {
    mportclose $mport
} -result "dlist pending successful."


test dlist_count_unmet {
    Dlist count unmet unit test.
} -setup {
    set mport [mportopen file://.]
    array set statusdict {
        vara 0
        varb 0
        varc 1
    }
    set tokens {vara varb vard}
} -body {
    if {[dlist_count_unmet $mport statusdict $tokens] != 3} {
        return "FAIL: wrong unmet number"
    }
    return "count unmet successful."
} -cleanup {
    mportclose $mport
} -result "count unmet successful."


# These just call the procs in macports_dlist namespace
# test ditem_create
# test ditem_delete
# test ditem_key
# test ditem_append
# test ditem_append_unique
# test ditem_contains


test dlist_append_dependents {
    Dlist append dependents unit test.
} -setup {
    set mport [mportopen file://.]
    ditem_key $mport requires [list a e]

    set result [list]
    set dlist [list]
    foreach dname {a b c d e f} {
        set $dname [ditem_create]
        ditem_key [set $dname] provides $dname
        lappend dlist [set $dname]
    }
    ditem_key $a requires [list b c]
    ditem_key $b requires [list e]
    lappend dlist $mport
} -body {
    set expected [lsort [list $mport $a $b $c $e]]
    set newlist [lsort [dlist_append_dependents $dlist $mport $result]]
    if {$newlist ne $expected} {
        return "FAIL: wrong depends: $newlist (expected $expected)"
    }
    return "Append depends successful."
} -cleanup {
    mportclose $mport
} -result "Append depends successful."


test dlist_get_next {
    Dlist get next unit test.
} -setup {
    set mport [mportopen file://.]
    set dict {gcc_select}
} -body {
    if {[dlist_get_next $mport $dict] ne $mport} {
        return "FAIL: wrong dlist"
    }
    return "dlist get next successful."
} -cleanup {
    mportclose $mport
} -result "dlist get next successful."


test dlist_eval {
    Dlist eval unit test.
} -setup {
    set mport [mportopen file://.]
    proc cond {arg} {
        if {[string match "ditem_*" $arg] != 0} {
            return 0
        } else {
            return 1
        }
    }
    proc handler {arg} {}
} -body {
    if {[dlist_eval $mport cond handler] ne ""} {
        return "FAIL: wrong value returned"
    }
    return "dlist eval successful."
} -cleanup {
    mportclose $mport
} -result "dlist eval successful."


test ditem_create {
    Ditem create unit test.
} -body {
    set res [macports_dlist::ditem_create]
    if {[string match "ditem_*" $res] == 0} {
        return "FAIL: ditem not created"
    }
    if {[info exists macports_dlist::$res] != 1} {
        return "FAIL: array not set"
    }
    return "Create ditem successful."
} -result "Create ditem successful."


test ditem_delete {
    Ditem delete unit test.
} -body {
    set res [macports_dlist::ditem_create]
    set res [macports_dlist::ditem_delete $res]
    if {[info exists macports_dlist::$res] != 0} {
        return "FAIL: array not deleted"
    }
    return "Ditem delete successful."
} -result "Ditem delete successful."


test ditem_key {
    Ditem key unit test.
} -setup {
    set res [macports_dlist::ditem_create]
    macports_dlist::ditem_key $res macports test
} -body {
    if {[dict get [set macports_dlist::$res]] ne "macports test"} {
        return "FAIL ditem key not set"
    }
    return "Ditem key successful."
} -cleanup {
    macports_dlist::ditem_delete $res
} -result "Ditem key successful."


test ditem_append {
    Ditem append unit test.
} -setup {
    set res [macports_dlist::ditem_create]
    macports_dlist::ditem_key $res macports test
    macports_dlist::ditem_append $res macports test2
} -body {
    if {[dict get [set macports_dlist::$res]] ne "macports {test test2}"} {
        return "FAIL: ditem key not appended"
    }
    return "Ditem key append successful."
} -cleanup {
    macports_dlist::ditem_delete $res
} -result "Ditem key append successful."


test ditem_append_unique {
    Ditem append unique unit test.
} -setup {
    set res [macports_dlist::ditem_create]
    macports_dlist::ditem_key $res macports test
    macports_dlist::ditem_append_unique $res macports test
} -body {
    if {[dict get [set macports_dlist::$res]] ne "macports test"} {
        return "FAIL: ditem key not unique"
    }
    return "Ditem append unique successful."
} -cleanup {
    macports_dlist::ditem_delete $res
} -result "Ditem append unique successful."


test ditem_contains {
    Ditem contains unit test.
} -setup {
    set mport [mportopen file://.]
} -body {
    if {[ditem_contains $mport provides] != 1} {
        return "FAIL: valid key not detected"
    }
    if {[ditem_contains $mport wants] != 0} {
        return "FAIL: invalid key detected"
    }
    return "ditem contains successful."
} -cleanup {
    mportclose $mport
} -result "ditem contains successful."


cleanupTests
